// https://leetcode.cn/problems/balanced-binary-tree/
public class BalancedTree {
    private static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        public TreeNode(int val)
        {
            this.val = val;
        }
    }
    class Solution {
        public boolean isBalanced(TreeNode root) {
            return getDepth(root) == -1 ? false :true;

        }

        private int getDepth(TreeNode node)
        {
            if(node == null) return 0;
            int left = getDepth(node.left);
            int right = getDepth(node.right);
            if(left == -1 || right == -1) return -1;
            return Math.abs(left-right) >1  ?  -1 : 1 + Math.max(left,right);

        }
    }
}
